﻿using System;
using System.Collections.Generic;

public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public string Class { get; set; }

    public Student(int id, string name, int age, string className)
    {
        Id = id;
        Name = name;
        Age = age;
        Class = className;
    }

    public override string ToString()
    {
        return $"学号: {Id}, 姓名: {Name}, 年龄: {Age}, 班级: {Class}";
    }
}

public class StudentManager
{
    private List<Student> _students = new List<Student>();

    public void AddStudent(Student student)
    {
        if (_students.Exists(s => s.Id == student.Id))
        {
            Console.WriteLine($"错误：学号为{student.Id}的学生已存在！");
            return;
        }
        _students.Add(student);
        Console.WriteLine($"学生{student.Name}添加成功！");
    }

    public void UpdateStudent(int id, Student newStudentInfo)
    {
        var existingStudent = _students.Find(s => s.Id == id);
        if (existingStudent == null)
        {
            Console.WriteLine($"错误：未找到学号为{id}的学生！");
            return;
        }

        if (newStudentInfo.Id != id && _students.Exists(s => s.Id == newStudentInfo.Id))
        {
            Console.WriteLine($"错误：新学号{newStudentInfo.Id}已被使用！");
            return;
        }

        existingStudent.Id = newStudentInfo.Id;
        existingStudent.Name = newStudentInfo.Name;
        existingStudent.Age = newStudentInfo.Age;
        existingStudent.Class = newStudentInfo.Class;
        Console.WriteLine($"学号为{id}的学生信息修改成功！");
    }

    public void DeleteStudent(int id)
    {
        var studentToRemove = _students.Find(s => s.Id == id);
        if (studentToRemove == null)
        {
            Console.WriteLine($"错误：未找到学号为{id}的学生！");
            return;
        }

        _students.Remove(studentToRemove);
        Console.WriteLine($"学号为{id}的学生已删除！");
    }

    public Student QueryStudent(int id)
    {
        return _students.Find(s => s.Id == id);
    }

    public void TraverseAllStudents()
    {
        if (_students.Count == 0)
        {
            Console.WriteLine("当前没有学生信息！");
            return;
        }

        Console.WriteLine("\n所有学生信息如下：");
        foreach (var student in _students)
        {
            Console.WriteLine(student);
        }
        Console.WriteLine();
    }
}

public class StudentTest
{
    public static void Main(string[] args)
    {
        StudentManager manager = new StudentManager();

        Console.WriteLine("===== 测试添加学生 =====");
        manager.AddStudent(new Student(1001, "张三", 18, "计算机一班"));
        manager.AddStudent(new Student(1002, "李四", 19, "计算机二班"));
        manager.AddStudent(new Student(1003, "王五", 18, "计算机一班"));
        manager.AddStudent(new Student(1001, "赵六", 20, "计算机三班"));
        manager.TraverseAllStudents();

        Console.WriteLine("===== 测试查询学生 =====");
        Student student = manager.QueryStudent(1002);
        Console.WriteLine(student != null ? student.ToString() : "未找到该学生");

        student = manager.QueryStudent(1004);
        Console.WriteLine(student != null ? student.ToString() : "未找到该学生（1004）");

        Console.WriteLine("===== 测试修改学生 =====");
        manager.UpdateStudent(1002, new Student(1002, "李四", 20, "计算机一班"));
        manager.UpdateStudent(1003, new Student(1005, "王五", 19, "计算机三班"));
        manager.UpdateStudent(1006, new Student(1006, "测试", 20, "测试班"));
        manager.TraverseAllStudents();

        Console.WriteLine("===== 测试删除学生 =====");
        manager.DeleteStudent(1001);
        manager.DeleteStudent(1004);
        manager.TraverseAllStudents();

        Console.ReadLine();
    }
}